package com.wonders.frame.autoGeneration.task;

import com.wonders.frame.autoGeneration.support.TaskContent;
import com.wonders.frame.autoGeneration.support.enums.ColumnTypeEnum;
import com.wonders.frame.autoGeneration.task.Core.Task;
import com.wonders.frame.common.util.FileUtil;
import com.wonders.frame.common.util.HumpUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * @author 顾乘瑞
 */
public class GenFormTask extends Task {
    private static final String PATH = System.getProperty("file.separator") +"src"
            + System.getProperty("file.separator") + "main"
            + System.getProperty("file.separator") + "java"
            + System.getProperty("file.separator") + "com"
            + System.getProperty("file.separator") + "wonders"
            + System.getProperty("file.separator") + "frame"
            + System.getProperty("file.separator") + "entity"
            + System.getProperty("file.separator") + "form"
            + System.getProperty("file.separator");
    private static final List<String> SKIP_COLUMN = Arrays.asList(
            "crt_user",
            "crt_date",
            "upd_user",
            "upd_date",
            "ID",
            "REMOVED",
            "CRT_USER",
            "CRT_DATE",
            "UPD_USER",
            "UPD_DATE"
    );

    public GenFormTask(final TaskContent cont) {
        super(cont);
    }

    @Override
    public Object call() {
        String tableName = cont.getTableName();
        String className = HumpUtil.tableName2ClassName(tableName) + "Form";

        String filePath = TaskContent.PROJECT_PATH + PATH;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String writeFilePath = filePath + className + ".java";
        StringBuilder content = new StringBuilder("package com.wonders.frame.entity.form;\n" +
                "\n" +
                "\n" +
                "import com.fasterxml.jackson.annotation.JsonFormat;\n" +
                "import lombok.Data;\n" +
                "import java.util.Date;\n" +
                "\n" +
                "@SuppressWarnings(\"serial\")\n" +
                "@Data\n");

        content.append("public class " + className + " {\n");
        cont.getColumns().forEach(v -> {
            if (SKIP_COLUMN.contains(v.getColumn())) {
                return;
            }
            content.append("    //" + v.getComment() + "\n");
            content.append("    private " + ColumnTypeEnum.valueOf(v.getType().toUpperCase()).getJavaType() + " " + HumpUtil.lineToHump(v.getColumn()) + ";\n\n");
        });
        content.append("\n}");
        FileUtil.createFile(content.toString(), writeFilePath);
        return null;
    }
}
